package lambdatext;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */



//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}


//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}


//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}


//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}



public class Textlambda {
    public static void main(String[] args) {
        HashMap<String,Integer> map =new HashMap<>();
        map.put("zhangsang",11);
        map.put("lisi",17);
        map.put("wangwu",14);
        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key:"+s+"  "+"value:"+integer);
            }
        });
        System.out.println("=================");
        map.forEach((String s,Integer i)-> System.out.println("key:"+s+"  "+"value:"+i));

    }
    public static void main10(String[] args) {
        ArrayList<String> list =new ArrayList<>();
        list.add("123");
        list.add("234");
        list.add("345");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("=========");
        list.forEach(s-> System.out.println(s));

     }

    public static void main9(String[] args) {
        PriorityQueue<Integer> queue1 = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        PriorityQueue<Integer> queue2 =new PriorityQueue<>(((o1, o2) -> o1.compareTo(o2)));

    }
    public static void main8(String[] args) {
       //int a=10;
        //a=100;
        NoParameterNoReturn noParameterNoReturn
                = () -> {
            int a=100;
            a=1;
            System.out.println(a);
            System.out.println("text()");
        };
        noParameterNoReturn.test();
        //a=100;
    }

    public static void main7(String[] args) {
        int a=10;
        //a=100;
        NoParameterNoReturn noParameterNoReturn =new NoParameterNoReturn() {
            @Override
            public void test() {
                //a=100;
                System.out.println(a);
                System.out.println("text()");
            }
        };
        //a=100;
    }
    public static void main6(String[] args) {
        MoreParameterReturn moreParameterReturn =
                (a,b)-> a+b;
        System.out.println(moreParameterReturn.test(10, 20));
    }
    public static void main5(String[] args) {
        OneParameterReturn oneParameterReturn =
                a -> a;
        System.out.println(oneParameterReturn.test(10));
    }
    public static void main4(String[] args) {
        NoParameterReturn noParameterNoReturn =
                ()->1;
        System.out.println(noParameterNoReturn.test());

    }
    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn
                =(a,b)-> System.out.println(a+b);
        moreParameterNoReturn.test(2,1);

    }
    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterNoReturn
                = a -> System.out.println(a);
        oneParameterNoReturn.test(10);
    }

    public static void main1(String[] args) {
        //没用lambda表达式
        NoParameterNoReturn noParameterNoReturn =new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("text()");
            }
        };
        noParameterNoReturn.test();
        System.out.println("===================");
        //用lambda表达式
//        NoParameterNoReturn noParameterNoReturn1 =()->{
//            System.out.println("text()");
//        };
        //优化
        NoParameterNoReturn noParameterNoReturn1 =()-> System.out.println("text()");

        noParameterNoReturn1.test();
    }
}
